/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of tools;


class Mouse extends InputMethod {
  static const String INPUT_TYPE    = "Mouse";
  
  static const int    BUTTONS       = 3;
  
  static const int    BUTTON_LEFT   = 0;
  static const int    BUTTON_MIDDLE = 1;
  static const int    BUTTON_RIGHT  = 2;
  
  final Vector2       position      = new Vector2();
  final Vector2       wheelDelta    = new Vector2();
  
  bool                mouseOver     = true;
  
  Mouse (Element element) : super(BUTTONS) {
    element.onMouseMove.listen(this._onMouseMove);
    element.onMouseDown.listen(this._onMouseDown);
    element.onMouseUp.listen(this._onMouseUp);
    element.onMouseWheel.listen(this._onMouseWheel);
    element.onMouseOver.listen(this._onMouseOver);
    element.onMouseOut.listen(this._onMouseOut);
  }
  
  /** See [InputMethod.update]. */
  void update () {
    this.wheelDelta.setNull();
    super.update();
  }
  
  bool isWheelUp () => this.wheelDelta.y > 0;
  
  bool isWheelDown () => this.wheelDelta.y < 0;
  
  void _onMouseMove (MouseEvent event) {
    this.position.x = event.offsetX;
    this.position.y = event.offsetY;
  }
  
  void _onMouseDown (MouseEvent event) {
    this.stateSet[event.button] = true;
  }
  
  void _onMouseUp (MouseEvent event) {
    this.stateUnset.add(event.button);
  }
  
  void _onMouseWheel (WheelEvent event) {
    this.wheelDelta.x += event.deltaX;
    this.wheelDelta.y += event.deltaY;
  }
  
  void _onMouseOver (MouseEvent event) {
    this.mouseOver = true;
  }
  
  void _onMouseOut (MouseEvent event) {
    this.mouseOver = false;
  }
}
